/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：绘制AB包管理器的窗口
* CreateTime：2020-07-10 13:03:57
* Version：1.0
* Modify Recorder：
*************************************************/

using System.Collections.Generic;
using UnityEditor;
using TinyToolKit;
using TinyToolKit.AssetLoad;      
using UnityEngine;

namespace TinyToolKitEditor
{
    public class DrawWindowAssetbundle : DrawWindowBase
    {
        private bool isFoldoutABAssetDic = true;
        private bool isFoldoutABItemDic = true;
        
        private TinyToolSettingAsset _toolSetting; 
        private TAssetBundleManager _assetBundleManager;

        public override void InitWindow(ScriptableObject setting)
        {
            _toolSetting = (TinyToolSettingAsset) setting;
        }

        protected override void DrawInternalMainGUI()
        {
            EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
            _toolSetting.isEditorMode = EditorGUILayout.Toggle("编辑器资源加载模式", _toolSetting.isEditorMode);
            EditorGUI.EndDisabledGroup();

            TinyToolWindowUtil.DrawDefaultEditorGUI();
            
            if (EditorApplication.isPlaying == false || TinyTool.Instance.HasManager<TAssetBundleManager>() == false) return;

            if (_assetBundleManager == null) _assetBundleManager = TinyTool.Instance.GetManager<TAssetBundleManager>();

            EditorGUILayout.Space();
            Dictionary<uint, AssetBundleItem> abItemDic = _assetBundleManager.AbItemDic;
            if (abItemDic != null && abItemDic.Count > 0) 
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                isFoldoutABItemDic = EditorGUILayout.Foldout(isFoldoutABItemDic, "Ab包缓存池");
                if (isFoldoutABItemDic)
                {
                    TinyToolWindowUtil.DrawTableHead("AB包", "状态");

                    foreach (var item in abItemDic)
                    {
                        string status = "引用次数：" + item.Value.refCount; 
                        EditorGUILayout.LabelField(item.Value.assetBundle.name, status);
                    }
                }
                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.Space();
            Dictionary<uint, ResourceItem> assetDic = _assetBundleManager.AssetDic;
            if (assetDic != null && assetDic.Count > 0)
            {
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                isFoldoutABAssetDic = EditorGUILayout.Foldout(isFoldoutABAssetDic, "资源缓存池");
                if (isFoldoutABAssetDic)
                {
                    TinyToolWindowUtil.DrawTableHead("资源", "状态");

                    foreach (var item in assetDic)
                    {
                        string abName = item.Value.abName;
                        string refCount = item.Value.refCount.ToString();
                        string status = $"AB包名：{abName}，引用次数：{refCount}";
                        if (item.Value.abDependencies.Count > 0)
                        {
                            string dependencies = string.Join("、", item.Value.abDependencies);
                            status += $"，依赖：{dependencies}"; 
                        }
                        EditorGUILayout.LabelField(item.Value.assetName,status);
                    }
                }
                EditorGUILayout.EndVertical();
            }
        }
    }
}

